public class PLPhaseLearn {
	PLLearnLayer[] mLearners;
	
	String mName;

	public PLPhaseLearn() {}
	public PLPhaseLearn(String name) {mName = name;}

	public String GetName() {return mName;}
	public void SetName(String name) {mName = name;}

	public void Learn() {

		if (!Init())
			return;
		
		PLFileQueryResponse file = ReadQRFile();
		if (file == null)
			return;		
		file.BeginRead();
		for (int i = 0; i < file.GetNumOfRecords(); ++i) {
			// read the next record			
			PLRecordQueryResponse record = file.GetNextRecord();
			if (null == record) {
				PLDebug.Warn("Incomplete records, read %d of %d", i, file.GetNumOfRecords());
				break;
			}

			if (null == record.GetQuery() || null == record.GetResponse()) {
				PLDebug.Warn("Bad QR record read at %d", i);
				continue;				
			}
			
			for (int j = 0; j < mLearners.length; ++j) {
				mLearners[j].Learn(record.GetQuery(), record.GetResponse());
			}
		}

		Finished();
		
	}


	private boolean Init() {
		if (! InitLayers()) {
			PLDebug.Warn("Could not init layers");
		}
		return true;
	}

	private boolean InitLayers() {
		mLearners = new PLLearnLayer[3];
		mLearners[0] = new PLLearnLayerFeature(GetName());
		mLearners[1] = new PLLearnLayerPattern(GetName());
		mLearners[2] = new PLLearnLayerCognition(GetName());	
		return true;
	}

	// inits the patterns from the file
	private PLFileQueryResponse ReadQRFile() {
		// first check if modified file exists, otherwise the base file
		String filename = PLFileUtils.GetQRFile(GetName());		
		if (!PLFileUtils.Exists(filename)) {
			PLDebug.Err("File %s does not exist", filename);
			return null;
		}

		// get the key value file name
		PLFileQueryResponse file = new PLFileQueryResponse(filename);
		file.BeginRead();
		
		// all done 	
		return file;
	}

	private boolean Finished() {
		for (int j = 0; j < mLearners.length; ++j) {
			mLearners[j].Finished();
		}
		return true;
	}



}



